<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/1
 * Time: 20:39
 */

class CommonCache{
    private static $memCache = null;
    private static $dataCacheQ = null;
    private static $authRedis = null;
    private static $cacheService = null;

    public static function getInstance($mem){
        if(empty($mem)){
            $mem = 'memCache';
        }
        if(self::$$mem == null){
            $class = ucfirst($mem)."Service";
            self::$$mem = new $class();
        }
        return self::$$mem;
    }
}

class CacheService{
    /*
     * 删除key
     */
    public function deleteCache($key){
        $key = 'Chat_'.$key;
        $this->mem->delete($key);
    }

    //设置缓存
    public function setCache($key,$data,$expireTime = 0){
        $key = 'Chat_'.$key;
        $this->mem->set($key,json_encode($data),$expireTime);//这里默认设置永不失效
    }

    //获取缓存
    public function getCache($key){
        if(empty($key)){
            return false;
        }
        $key = 'Chat_'.$key;
        $data = json_decode($this->mem->get($key),true);
        if($data){
            return $data;
        }else{
            return false;
        }
    }
}

class MemCacheService extends CacheService{
    public $mem = '';
    public $memName = 'memCache';
    public function __construct(){
        $mem = new Memcache;
        $mem->connect("127.0.0.1", 11211);
        $this->mem = $mem;
    }
}

class DataCacheQService extends CacheService{
    public $mem = '';
    public $memName = 'dataCacheQ';
    public function __construct($game){
        $memQ = new Memcache();
        $memQ->connect('localhost', 22201);
        $this->mem = $memQ;
    }
}

class AuthRedisService extends CacheService{
    public $mem = '';
    public $memName = 'authRedis';
    public function __construct(){
        $redis = new redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->auth("xiaohuihui"); //这里如果redis安装的时候，没有设置密码的话，注释掉，如果需要密码的话，改成自己所要的密码
        $this->mem = $redis;
    }
}